#include "stm32f10x.h"  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Motor.h"
#include "MatrixKey.h"
#include "RED.h"
#include "DHT11.h"
#include <string.h>
#include "Timer.h"
#include "Serial.h"

/*
按键	
OLED	SCL:PB8 SDA:PB9
TB6612	PWMA:PA2 AIN1:PA4 AIN2:PA5 AO1、AO2:接电机
温湿度	PA8
红外	PA1
蓝牙	RX:PA9 TX:PA10
*/



char RxData;					//接收蓝牙数据

uint8_t KeyNum,RED_Data,RH,TH;		//分别用于获取按键，红外，温度，湿度
int8_t Speed;		//风扇速度
uint8_t Key_M = 1,Time_demo;		//用于切换模式
uint8_t Time_1,Time_10,Time;		//个位和十位的时间,总时间
uint8_t temperature = 30;	//阈值设置
int8_t dang = 0;			//档数
uint8_t Num = 1,Timedemo;	//标志位


int main(void)
{
	/*模块初始化*/
	OLED_Init();		//OLED初始化
	Motor_Init();
	DHT11_Init();
	MatrixKey_Init();
	Red_Init();
	Timer_Init();
	Serial_Init();
	OLED_ShowChinese(0,0,"温度");
	OLED_ShowChinese(16*4,0,"湿度");
	
	//OLED_ShowChinese(0,16,"模式");
	//OLED_ShowChinese(16*4,16,"档数");
	//OLED_ShowNum(16*2,16,Key_M,1,OLED_8X16);
	//OLED_ShowNum(16*6,16,dang,1,OLED_8X16);
	
	OLED_ShowChinese(0,16*2,"速度：");
	OLED_ShowSignedNum(16*3,16*2,Speed,3,OLED_8X16);
	
	//OLED_ShowChinese(0,16*3,"倒计时：");
	//OLED_ShowNum(16*4,16*3,Time,2,OLED_8X16);
	//OLED_Update();

	while (1)
	{
		DHT11_Read_Data(&TH,&RH);//获取当前温湿度
		OLED_ShowNum(16*2,0,TH,2,OLED_8X16);
		OLED_ShowNum(16*6,0,RH,2,OLED_8X16);
		OLED_Update();
		if(Timedemo == 1)
		{
			if(RED_Data == 1)	//如果有人
			{
				OLED_ShowString(16*6,16*2,"Hi! ",OLED_8X16);
				if(temperature < TH)	//实际温度高于设置阈值
				{
					Speed = (TH-temperature)*20;	//每高一度速度+20
					if(Speed > 100)
					{
						Speed = 100;
					}
				}
				else Speed = 0;			
			}
			else
			{
				OLED_ShowString(16*6,16*2,"Bey!",OLED_8X16);
				Speed = 0;
			}
			OLED_ShowSignedNum(16*3,16*2,Speed,3,OLED_8X16);			
			
			OLED_Update();
			Motor_SetSpeed(Speed);
		}
		Delay_ms(500);

	}
}


uint16_t count;
uint8_t yy;

void TIM1_UP_IRQHandler(void)		//每1毫秒进一次中断
{
	if (TIM_GetITStatus(TIM1, TIM_IT_Update) == SET)
	{
		count++;
		if(count == 1000)
		{
			count = 0;
			if(Time_demo == 0 && Time > 0)	//进入倒计时模式
			{
				Time--;
				if(Time == 0)		//倒计时结束
				{
					Timedemo = 0;	//关闭自动挡
					Speed = 0;		//速度置0
					dang = 0;		//去除记录
					Time_1 = 0;
					Time_10 = 0;
					Motor_SetSpeed(0);
					OLED_ShowSignedNum(16*3,16*2,Speed,3,OLED_8X16);			
					OLED_ShowNum(16*6,16*1,dang,1,OLED_8X16);
				}
				OLED_ShowNum(16*4,16*3,Time,2,OLED_8X16);
				OLED_Update();
				
			}
		}
		if(count%20 == 0)	//获取按键值
		{
			RED_Data = Red_Rdata();		//检测是否有人
			if(RxData)		//
			{
				KeyNum = RxData-'0';//
				RxData = 0;
//				OLED_ShowNum(16*6,16*3,KeyNum,2,OLED_8X16);
//				OLED_Update();
			}
			else
			{
				KeyNum = Key_LoopY();
			}
			
			if(KeyNum)		//如果按键按下
			{
				if(KeyNum == 3)				//切换模式键 Key_M = 1:手动，Key_M = 2:自动
				{
					Num = 1;		//手动自动模式
					Key_M += 1;
					if(Key_M > 2){Key_M = 1;}
					
					OLED_ShowNum(16*2,16,Key_M,1,OLED_8X16);
					OLED_Update();
				}
				if(Num == 1)		//手动自动模式
				{
					if(Key_M == 1)			//模式1手动挡模式
					{
						Timedemo = 0;
						OLED_ClearArea(16*3,16,16*8,16);
						OLED_ShowChinese(16*4,16,"档数");
						
						if (KeyNum == 1)	//减档
						{
							dang--;
							if(dang < 0)
							{
								dang = 5;
							}
						}
						if (KeyNum == 2)	//加档
						{
							dang++;
							if(dang > 5)
							{
								dang = 0;
							}
						}
						Speed = dang*20;
						OLED_ShowString(16*6,16*2,"    ",OLED_8X16);
						OLED_ShowNum(16*6,16*1,dang,1,OLED_8X16);
						OLED_ShowSignedNum(16*3,16*2,Speed,3,OLED_8X16);			
						
						OLED_Update();
						Motor_SetSpeed(Speed);

					}
					if(Key_M == 2)
					{
						Timedemo = 1;
						dang = 0;
						OLED_ClearArea(16*3,16,16*8,16);
						OLED_ShowChinese(16*4,16,"阈值");

						if (KeyNum == 1)	//温度阈值--
						{
							temperature--;
							if(temperature < 16)
							{
								temperature = 16;
							}
						}
						if (KeyNum == 2)	//温度阈值++
						{
							temperature++;
							if(temperature > 40)
							{
								temperature = 40;
							}
						}
						OLED_ShowNum(16*6,16*1,temperature,2,OLED_8X16);
						OLED_Update();
					}
				}
				if(KeyNum == 4)		//切换倒计时模式
				{
					Num = 2;
					Time_demo++;
					if(Time_demo == 2)	//Time_demo=2相当于确定键
					{
						Num = 1;
						Time_demo = 0;
						OLED_ShowString(16*5+8,16*3,"  ",OLED_8X16);
						
						OLED_Update();
					}
				}
				if(Num == 2)
				{
					if(Time_demo == 1)	//输入需要倒计时的时间
					{
						if(KeyNum == 1)
						{
							Time_10++;
							if(Time_10 > 9)
							{
								Time_10 = 0;
							}
						}
						if(KeyNum == 2)
						{
							Time_1++;
							if(Time_1 > 9)
							{
								Time_1 = 0;
							}
						}
						Time = Time_10*10 + Time_1;
						OLED_ShowNum(16*4,16*3,Time,2,OLED_8X16);
						OLED_ShowString(16*5+8,16*3,"<-",OLED_8X16);
						
						OLED_Update();
					}
				}
			}
		
		}
		
		
		TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
	}
}
uint8_t RxData_demo,RxData_de,RxState;

void USART1_IRQHandler(void)
{
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)		//判断是否是USART1的接收事件触发的中断
	{
		RxData = USART_ReceiveData(USART1);
//		RxData_demo = USART_ReceiveData(USART1);				//读取数据寄存器，存放在接收的数据变量
//		if(RxState == 0)
//		{
//			if(RxData_demo == 0xA5 && RxData == 0)		//包头
//			{
//				RxState = 1;
//			}
//		}
//		else if(RxState == 1)
//		{
//			RxData_de = RxData_demo;
//			RxState = 2;
//		}
//		else if(RxState == 2)
//		{
//			if(RxData_demo == 0x5A)		//包尾
//			{
//				RxState = 3;
//			}
//		}
//		if(RxState == 3)
//		{
//			RxState = 0;
//			RxData = RxData_de;
//		}

		USART_ClearITPendingBit(USART1, USART_IT_RXNE);			//清除USART1的RXNE标志位
																//读取数据寄存器会自动清除此标志位
																//如果已经读取了数据寄存器，也可以不执行此代码
	}
}